<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/10/31 0031
 * Time: 下午 1:58
 */

namespace app\mobile\controller;


use app\admin\model\CategoryModule;
use app\admin\model\Lesson;
use think\Db;

class Apply extends MoBase
{
    public function index(){
        $id = $this->request->param('id');
        $lesson = Lesson::get($id);
        if ($lesson['type']==39){
            //培训报名
            $pcate = CategoryModule::get(39);
            $this->assign([
                'pcate'=>CategoryModule::get($pcate['pid']),
                'ppcate'=>$pcate,
                'cur'=>['name'=>'报名咨询','id'=>''],
                'lesson_id'=>$id
            ]);
            return view();
        }elseif ($lesson['type']==12){
            //课程报名
            $pcate = CategoryModule::get(12);
            $this->assign([
                'pcate'=>$pcate,
                'ppcate'=>$pcate,
                'cur'=>['name'=>'课程报名','id'=>''],
                'lesson_id'=>$id
            ]);
            return view('entry');
        }
    }

    //培训报名
    public function apply2(){
        if ($this->request->isPost()){
            $post = $this->request->post('','','htmlspecialchars');
            $arr = ['name','sex','phone','mail','collect','company','content','lesson_id'];
            foreach ($arr as $v){
                if (empty($post[$v])){
                    return ['code'=>$v];
                }
                if ($v=='phone' && !preg_phone($post[$v])){
                    return ['code'=>$v];
                }
            }
            $lesson_id = $post['lesson_id'];
            unset($post['lesson_id']);
            if (cache($this->request->ip())!=$lesson_id){
                Db::name('msg')->insert([
                    'create_at'=>date('Y-m-d H:i:s'),
                    'content'=>json_encode($post,320),
                    'type'=>2,
                    'lesson_id'=>$lesson_id
                ]);
                cache($this->request->ip(),$lesson_id,300);
                return ['code'=>1];
            }else{
                return ['code'=>'busy'];
            }

        }
    }

    //课程报名
    public function apply3(){

        if ($this->request->isPost()){
            $post = $this->request->post('','','htmlspecialchars');
            $arr = ['name','sex','type','link','age','school','content','lesson_id'];
            foreach ($arr as $v){
                if (empty($post[$v])){
                    return ['code'=>$v];
                }
            }
            $lesson_id = $post['lesson_id'];
            unset($post['lesson_id']);
            if (cache($this->request->ip())!=$lesson_id){
                Db::name('msg')->insert([
                    'create_at'=>date('Y-m-d H:i:s'),
                    'content'=>json_encode($post,320),
                    'type'=>4,
                    'lesson_id'=>$lesson_id
                ]);
                cache($this->request->ip(),$lesson_id,300);
                return ['code'=>1];
            }else{
                return ['code'=>'busy'];
            }

        }
    }

    //合作咨询
    public function apply4(){
        if ($this->request->isPost()){
            $post = $this->request->post('','','htmlspecialchars');
            $arr = ['name','tel','type','company','content'];
            foreach ($arr as $v){
                if (empty($post[$v])){
                    return ['code'=>$v];
                }
            }
            if (empty(cache($this->request->ip()))){
                Db::name('msg')->insert([
                    'create_at'=>date('Y-m-d H:i:s'),
                    'content'=>json_encode($post,320),
                    'type'=>3,
                ]);
                cache($this->request->ip(),'123',300);
                return ['code'=>1];
            }else{
                return ['code'=>'busy'];
            }

        }
    }

    //未名学院
    public function apply5(){
        if ($this->request->isPost()){
            $post = $this->request->post('info','','htmlspecialchars');
            $arr = ['name','tel'];
            foreach ($arr as $v){
                if (empty($post[$v])){
                    return ['code'=>$v];
                }
            }
            if (empty(cache($this->request->ip()))){
                Db::name('msg')->insert([
                    'create_at'=>date('Y-m-d H:i:s'),
                    'content'=>json_encode($post,320),
                    'type'=>4,
                ]);
                cache($this->request->ip(),'123',300);
                return ['code'=>1];
            }else{
                return ['code'=>'busy'];
            }
        }
    }
}